package LeetCode_100;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2022/10/26 21:13
 * //相同的树
 */
class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p==null &&q==null){
            return true;
        }
        //只能是：
        //1. p == null && q!=null
        //2. p != null && q==null
        if(p == null || q ==null){
            return false;
        }
        //3. p!=null && q!==null
        //相同： p.val == q.val , q->left == q -> left
        if (p.val != q.val){
            return false;//根不同
        }
        if (!isSameTree(p.left,q.left)){
            return false;//左子树不同
        }
        if (!isSameTree(p.right,q.right)){
            return false;//右子树不同
        }
        //走到这里，根相同，左右子树相同，返回true
        return true;
        //短路的写法
        //return p.val == q.val && isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
    }
}

class TreeNode {
      int val;
      TreeNode left;
     TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
